package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.quest.JsonResult;
import cn.wolfcode.quest.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//生成Bean对象
@Controller
//设置虚拟名
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //方法显示
    @RequestMapping("/list")
    @RequiredPermission(name = "部门列表", expression = "department:list")
    public String listAll(Model model, QueryObject qo) {

        //查询出Result对象
        PageInfo<Department> pageInfo = service.query(qo);

        //添加到作用域
        model.addAttribute("pageResult", pageInfo);
        //跳转
        System.out.println(1);
        return "department/list";
    }

    //方法跳转到添加页面
    @RequiredPermission(name = "部门编辑", expression = "department:input")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        //判断id是否有值,有则查询出对应数据,放入作用域
        if (id != null) {
            Department department = service.get(id);
            model.addAttribute("department", department);
        }
        return "department/input";
    }

    //方法保存或修改
    @RequiredPermission(name = "部门保存或更新", expression = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        //判断department是否有id,有为修改无为添加
        System.out.println(department);
        System.out.println("进入方法");
      /*  if (department.getId() != null) {
            service.update(department);
        } else {
            service.insert(department);
        }
          */

       JsonResult jsonResult=new JsonResult();

           try{

               if (department.getId() != null) {
                   service.update(department);
               } else {
                   service.insert(department);
               }
               jsonResult.setSuccess(true);
               jsonResult.setMsg("添加或者更新成功");
               return new JsonResult();

           }catch (Exception e){
                    e.printStackTrace();
                    jsonResult.setSuccess(false);
                    jsonResult.setMsg("添加或者更新失败");
                     return jsonResult;
           }



    }

    //方法删除
    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission(name = "部门删除", expression = "department:delete")
    public JsonResult delete(Long id) {

        //调用业务方法,删除数据库数据
        try {
            service.deleteById(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            JsonResult jsonResult = new JsonResult();
            jsonResult.setSuccess(false);
            jsonResult.setMsg("添加失败");
            return jsonResult;
        }
    }
}
